import subprocess
import re
import ddddocr

def get_sms():
    try:
        # 执行ADB命令获取短信
        result = subprocess.run(
            ["adb", "shell", "content", "query", "--uri", "content://sms/inbox", "--projection", "address,body,date"],
            capture_output=True,
            text=True
        )
        return result.stdout
    except Exception as e:
        print(f"获取短信失败: {e}")
        return None


def parse_sms(sms_data):
    messages = sms_data.split("\n")
    for message in messages:
        if "body=" in message:
            # 提取短信内容
            body_start = message.index("body=") + len("body=")
            body_end = message.index(",", body_start)
            body = message[body_start:body_end]
            # 提取发送者号码
            address_start = message.index("address=") + len("address=")
            address_end = message.index(",", address_start)
            address = message[address_start:address_end]
            yield address, body


# 示例：获取并解析短信
sms_data = get_sms()
if sms_data:
    for address, body in parse_sms(sms_data):
        print(f"发送者: {address}, 内容: {body}")

def extract_verification_code(sms_body):
    # 假设验证码为6位数字
    code = re.search(r'\b\d{6}\b', sms_body)
    return code.group(0) if code else None





def ocr_verification_code(image_path):
    ocr = ddddocr.DdddOcr()
    with open(image_path, 'rb') as f:
        image = f.read()
    return ocr.classification(image)


from flask import Flask, request

app = Flask(__name__)


@app.route('/forward', methods=['POST'])
def forward():
    data = request.json
    code = data.get('code')
    # 转发验证码到目标地址
    # 例如：requests.post(url, json={'code': code})
    return {'status': 'success', 'message': f'验证码 {code} 已转发'}


if __name__ == '__main__':
    app.run(port=8000)
